package net.xfok.info.sinafinance;

import java.util.Date;
import java.util.Random;

import android.app.PendingIntent;
import android.appwidget.AppWidgetManager;
import android.appwidget.AppWidgetProvider;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.util.Log;
import android.widget.RemoteViews;

public class InfoWidget extends AppWidgetProvider{
	private static final String TAG="InfoWidget";
	private RemoteViews views;
	private Context mContext;
	
	public void onUpdate(Context context, AppWidgetManager appWidgetManager, int[] appWidgetIds){
		Log.i(TAG, "onUpdate");
		initWidget(context);
		updateWidget(appWidgetManager, appWidgetIds);
		super.onUpdate(context, appWidgetManager, appWidgetIds);		
	}
	
	public void onReceive(Context context, Intent intent){
		initWidget(context);
		String action=intent.getAction();
		Log.i(TAG, "onReceive:======>"+action);
		if(action.equals(InfoConstant.ACTION_UPDATE_WIDGET)){
			updateWidget(null, null);
		}
		super.onReceive(context, intent);
	}
	
	private void initWidget(Context context){
		mContext=context;
		if(views==null){
			views=new RemoteViews(context.getPackageName(), R.layout.widget_layout);
		}
	}
	
	private void updateWidget(AppWidgetManager appWidgetManager,int[] appWidgetIds){
		Log.i(TAG, "==========>updateWidget");
		views.setTextViewText(R.id.w_title,mContext.getString(R.string.app_name)+"："+mContext.getString(R.string.dialog_message_loading) );
		views.setTextViewText(R.id.w_summary,mContext.getString(R.string.label_summary)+mContext.getString(R.string.dialog_message_loading));
		views.setTextViewText(R.id.w_date,InfoConstant.dateToStr(new Date()));	
		views.setImageViewResource(R.id.w_icon, R.drawable.ic_new);
		InfoItem info=InfoManage.findFirstInfo(mContext);
//		for(int i=0;i<appWidgetIds.length;i++){
			//start a activity
		Intent intent=new Intent(mContext,InfoList.class);
		PendingIntent pendingIntent=PendingIntent.getActivity(mContext, 0, intent, 0);
		views=new RemoteViews(mContext.getPackageName(), R.layout.widget_layout);
		views.setOnClickPendingIntent(R.id.widget, pendingIntent);
		if(info!=null){
			switch (info.getStatus()) {
				case InfoConstant.STATUS_NEW:
					views.setImageViewResource(R.id.w_icon, R.drawable.ic_new);
					break;
				case InfoConstant.STATUS_READ:
					views.setImageViewResource(R.id.w_icon, R.drawable.ic_read);
					break;
				default:
					views.setImageViewResource(R.id.w_icon, R.drawable.ic_new);
					break;
			}
			views.setTextViewText(R.id.w_title, mContext.getString(R.string.app_name)+":"+info.getTitle());
			views.setTextViewText(R.id.w_summary,Html.fromHtml(mContext.getString(R.string.label_summary)+info.getSummary()));
			views.setTextViewText(R.id.w_date,info.getPubDate());
		}else{
			mContext.startService(new Intent(InfoConstant.INFO_SERVICE));			
		}	
		views.setOnClickPendingIntent(R.id.w_title, pendingIntent);
		views.setOnClickPendingIntent(R.id.w_summary, pendingIntent);			
//			//start a service
//			intent=new Intent("net.xfok.widget.MyService");	
//			pendingIntent=PendingIntent.getService(mContext, 0, intent, 0);
//			if(flag)views.setTextViewText(R.id.w_date_panel, randStr());
//			views.setOnClickPendingIntent(R.id.w_date_panel, pendingIntent);
//			//send a broadcast
//			intent=new Intent("net.xfok.widget.MyBroadcast");	
//			pendingIntent=PendingIntent.getBroadcast(mContext, 0, intent, 0);
//			if(flag)views.setTextViewText(R.id.w_summary, randStr());
//			views.setOnClickPendingIntent(R.id.w_summary, pendingIntent);				
//		}
//		flag=false;
		if (appWidgetManager == null)
			appWidgetManager = AppWidgetManager.getInstance(mContext);
		if (appWidgetIds != null)
			appWidgetManager.updateAppWidget(appWidgetIds, views);
		else
			appWidgetManager.updateAppWidget(new ComponentName(mContext, InfoWidget.class), views);
	}
	
	private String randStr(){
		String[] strs={"Cake","Back","Think","Good","Beef"};
		Random ran=new Random();
		return strs[ran.nextInt(strs.length)];
	}
}